
let user=require('../model/user.js')   //引入模型

async function creatUser({name,password}){
  let res=await user.create({name,password})    //模型。create({}) 创建数据
  return res.dataValues
}

async function getUser({id,name,password,isAdmin}){
    let whereOpt={}
    id&&(whereOpt.id=id)
    name&&(whereOpt.name=name)
    password&&(whereOpt.password=password)
    isAdmin&&(whereOpt.isAdmin=isAdmin)
    let res=await user.findOne({
        where:whereOpt,
        attributes:['id','name','password','isAdmin']
    })
    return res?res.dataValues:null
}

async function updateUser({id,name,password,isAdmin}){
   let whereOpt={}
   id&&(whereOpt.id=id)
   name&&(whereOpt.name=name)
   password&&(whereOpt.password=password)
   isAdmin&&(whereOpt.isAdmin=isAdmin)
   let res=await user.update(whereOpt,{where:{name}})

   return res[0]>0?true:false
}
module.exports={
    creatUser,
    getUser,
    updateUser
}